<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateManageAuthModuleTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('manage_authorities', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 20)->comment('权限名称');
            $table->string('uri', 100)->unique()->comment('权限name');
            $table->string('description', 255)->nullable()->comment('权限描述');
            $table->timestamps();
        });

        Schema::create('manage_menus', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('parent_id')->default(0)->comment('上级菜单');
            $table->string('name', 100)->comment('菜单程度名称');
            $table->string('title', 100)->comment('菜单标题');
            $table->string('icon', 50)->nullable()->comment('菜单图标');
            $table->tinyInteger('sort', false, true)->comment('排序');
            $table->string('description', 255)->nullable()->default('')->comment('权限描述');
            $table->timestamps();
        });

        Schema::create('manage_menu_authority', function (Blueprint $table) {
            $table->unsignedInteger('menu_id');
            $table->unsignedInteger('authority_id');

            $table->foreign('menu_id')->references('id')->on('manage_menus')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('authority_id')->references('id')->on('manage_authorities')
                ->onUpdate('cascade')->onDelete('cascade');

            $table->primary(['menu_id', 'authority_id']);
        });

        Schema::create('manage_roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 20)->comment('角色名称');
            $table->string('description', 255)->nullable()->default('')->comment('角色描述');
            $table->timestamps();

        });

        Schema::create('manage_role_menu', function (Blueprint $table) {
            $table->unsignedInteger('menu_id');
            $table->unsignedInteger('role_id');

            $table->foreign('menu_id')->references('id')->on('manage_menus')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('manage_roles')
                ->onUpdate('cascade')->onDelete('cascade');

            $table->primary(['menu_id', 'role_id']);
        });

        Schema::create('manage_role_authority', function (Blueprint $table) {
            $table->unsignedInteger('authority_id');
            $table->unsignedInteger('role_id');

            $table->foreign('authority_id')->references('id')->on('manage_authorities')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('manage_roles')
                ->onUpdate('cascade')->onDelete('cascade');

            $table->primary(['authority_id', 'role_id']);
        });

        Schema::create('manage_users', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('role_id')->nullable()->comment('所属角色');
            $table->string('name', 20)->comment('姓名');
            $table->char('phone', 11)->comment('手机号码');
            $table->string('email', 50)->unique()->comment('邮件');
            $table->string('password', 100)->comment('密码');
            $table->string('remark')->nullable()->comment('用户说明');
            $table->tinyInteger('status')->default(0)->comment('状态');
            $table->timestamps();

            $table->foreign('role_id')->references('id')->on('manage_roles')
                ->onUpdate('cascade')->onDelete('restrict');
        });

        Schema::create('manage_logs', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id')->nullable()->comment('所属用户');
            $table->string('name')->comment("path info");
            $table->string('method', 10)->comment('请求类型');
            $table->timestamps();
        });

        DB::statement("ALTER TABLE `manage_authorities` COMMENT '权限表'");
        DB::statement("ALTER TABLE `manage_menus` COMMENT '菜单表'");
        DB::statement("ALTER TABLE `manage_roles` COMMENT '角色表'");
        DB::statement("ALTER TABLE `manage_users` COMMENT '用户表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('manage_users');
        Schema::dropIfExists('manage_role_authority');
        Schema::dropIfExists('manage_role_menu');
        Schema::dropIfExists('manage_roles');
        Schema::dropIfExists('manage_menu_authority');
        Schema::dropIfExists('manage_menus');
        Schema::dropIfExists('manage_authorities');
        Schema::dropIfExists('manage_logs');
    }
}
